import * as Cesium from 'cesium'
class ImageryThemeGL {
    /**
     *
     * @param {{
     *  name: string
     *  bgColor?: Cesium.Color
     *  alpha?: number
     *  invert?: boolean
     * }} options
     */
    constructor(options) {
        this.params = options;

        this.shaders = {
            bgFS: `
                uniform sampler2D u_map;
                uniform vec4 u_bgColor;
                varying vec2 v_textureCoordinates;
                void main(){
                    vec2 uv = v_textureCoordinates;
                    vec4 bgColor = u_bgColor;
                    vec4 color = texture2D( u_map , uv );
                    if( color.a > 0. ){
                        //  gl_FragColor = bgColor;
                        // gl_FragColor = color;
                        // gl_FragColor = vec4( vec3( 1.-color.r,1.-color.g,1.-color.b ) , 1 );//颜色反转
                    }
                }
                 `,
            mainFS: `
                    uniform sampler2D u_map;
                    uniform bool u_invert;
                    uniform float u_alpha;
                    
                    varying vec2 v_textureCoordinates;
                    void main(){
                        vec2 uv = v_textureCoordinates;
                        vec4 color = texture2D( u_map , uv );
                        //计算灰度
                        float grayVal =( color.r + color.g + color.b ) / 3.;
                        //灰度反转
                        if(u_invert){
                            grayVal = 1. - grayVal;
                        }
                        //应用前景透明度，以便和背景混合
                        float alpha = color.a * u_alpha;
                        gl_FragColor=vec4( vec3( grayVal ) , alpha );
                    
                        float r=gl_FragColor.r;
                        float g=gl_FragColor.g;
                        float b=gl_FragColor.b;
                    
                        if(r>0.5&&g>0.5&&b>0.5)
                        {
                            gl_FragColor.a = gl_FragColor.a - 0.3;
                            gl_FragColor.b = gl_FragColor.b + 0.7;
                            gl_FragColor.g = gl_FragColor.g + 0.4;
                        }else
                        {
                            gl_FragColor.a=gl_FragColor.a-0.2;
                            gl_FragColor.a = gl_FragColor.a - 0.3;
                            gl_FragColor.b = gl_FragColor.b + 0.5;
                            gl_FragColor.g = gl_FragColor.g + 0.2;
                        }
                    }
                `,//`这里贴处理瓦片图像的shader代码`,
            uniformMap: {
                u_invert() {
                    return true;
                },
                u_alpha() {
                    return 1;//options.alpha;
                },
                u_bgColor() {
                    return Cesium.Color.fromCssColorString("#1FC6FF");//options.bgColor;
                },
            },
        };
    }

    /**
     *
     * @param {Cesium.Context} context
     * @param {Cesium.Texture} texture
     * @returns {Cesium.Texture}
     */
    processTexture(context, texture) {
        const { params, shaders } = this;
        const sampler = texture.sampler;
        const {
            Texture,
            PixelFormat,
            PixelDatatype,
            WebGLConstants,
            Framebuffer,
            RenderState,
            BlendingState,
            Color,
            // defined,
        } = Cesium;

        //创建处理后的瓦片贴图，作为RTT的目标纹理
        var textureProcessed = new Texture({
            context: context,
            pixelFormat: PixelFormat.RGBA,
            pixelDatatype: PixelDatatype.UNSIGNED_BYTE,
            sampler: sampler,
            width: texture.width,
            height: texture.height,
            flipY: texture.flipY,
            target: WebGLConstants.TEXTURE_2D,
            preMultiplyAlpha: params.preMultiplyAlpha,
        });
        var framebuffer = new Framebuffer({
            context: context,
            colorTextures: [textureProcessed],
            destroyAttachments: false,
        });
        const renderState = RenderState.fromCache({
            depthTest: {
                enabled: false,
            },
            blending: params.preMultiplyAlpha
                ? BlendingState.PRE_MULTIPLIED_ALPHA_BLEND
                : BlendingState.ALPHA_BLEND,
            viewport: {
                x: 0,
                y: 0,
                width: texture.width,
                height: texture.height,
            },
        });

        //传递原始瓦片贴图
        shaders.uniformMap.u_map = function () {
            return texture;
        };

        //画背景
        if (!Color.equals(params.bgColor, Color.TRANSPARENT)) {
            // console.log(context, "bgcommand");
            var bgCommand = context.createViewportQuadCommand(shaders.bgFS, {
                framebuffer: framebuffer,
                renderState: renderState,
                uniformMap: shaders.uniformMap,
            });
            bgCommand.execute(context);
        }

        // //瓦片加工
        var mainCommand = context.createViewportQuadCommand(shaders.mainFS, {
            framebuffer: framebuffer,
            renderState: renderState,
            uniformMap: shaders.uniformMap,
        });
        mainCommand.execute(context);

        framebuffer.destroy();
        texture.destroy();

        return textureProcessed;
    }
}

export default ImageryThemeGL